const fs = require("fs");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ProgressBarPlugin = require("progress-bar-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const babelConfig = require("./babelConfig")();
const pkg = require("../package.json");

const isDev = process.env.NODE_ENV === "development";

if (!isDev) {
  fs.writeFileSync(`./release`, pkg.version, "utf8");
}

let commonConfig = {
  entry: {
    index: path.resolve(__dirname, "../src/index.jsx"),
  },
  output: {
    filename: "static/js/[name].[chunkhash:4].js",
    path: path.resolve(__dirname, "../dist"),
    // publicPath: isDev ? "/" : "./",
    clean: true,
  },
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "../src"),
    },
    extensions: [".jsx", ".js", ".json"],
  },
  cache: {
    type: isDev ? "memory" : "filesystem"
  },
  module: {
    rules: [{
        test: /\.jsx?$/,
        use: {
          loader: "babel-loader",
          options: babelConfig,
        },
        include: [
          path.resolve(__dirname, "../src"),
          // path.resolve(__dirname, "../node_modules/crypto-js"),
        ],
        exclude: /node_modules/,
      },
      {
        test: /\.(css|scss)$/,
        use: [
          isDev ? "style-loader" : MiniCssExtractPlugin.loader,
          "css-loader",
          {
            loader: "postcss-loader",
            options: !isDev ?
              {
                postcssOptions: {
                  plugins: [
                    ["autoprefixer", {
                      grid: true
                    }]
                  ],
                },
              } :
              {},
          },
          "sass-loader",
        ],
      },
      {
        test: /\.less$/,
        use: [
          { loader: "style-loader" },
          { loader: "css-loader" },
          {
            loader: "less-loader",
            options: {
              modifyVars: {},
              javascriptEnabled: true,
            },
          },
        ],
      },
      {
        test: /\.(png|jpe?g|gif|bmp|svg)$/,
        exclude: isDev ? /node_modules/ : undefined,
        // include: [path.resolve(__dirname, "../src")],
        type: "asset",
        parser: {
          dataUrlCondition: {
            maxSize: 8 * 1024,
          },
        },
        generator: {
          filename: isDev ?
            "static/images/[name][ext]" :
            "static/images/[name].[hash:4][ext]",
        },
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        exclude: isDev ? /node_modules/ : undefined,
        // include: [path.resolve(__dirname, "../src")],
        type: "asset",
        generator: {
          filename: isDev ?
            "static/fonts/[name][ext]" :
            "static/fonts/[name].[hash:4][ext]",
        },
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        exclude: isDev ? /node_modules/ : undefined,
        // include: [path.resolve(__dirname, "../src")],
        type: "asset",
        generator: {
          filename: isDev ?
            "static/videos/[name][ext]" :
            "static/videos/[name].[hash:4][ext]",
        },
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      time: new Date().getTime(),
      template: path.resolve(__dirname, "../public/index.html"),
      chunks: ["index"],
      baseApi: isDev ? "/api" : "/",
    }),
    new ProgressBarPlugin(),
  ],
};

module.exports = commonConfig;
